var app = angular.module('myApp', []);
function cont($scope, $http,$window,$filter) 
{		

	$scope.currentPage = 0;	
	$scope.pageSize = 75;
	$scope.count = 0;
	$scope.Math = window.Math;
	$scope.parentadd = false;
	$scope.myValue = false;
	$scope.chk1 = false;	$scope.chk2 = false;	$scope.chk3 = false;	
	document.getElementById("bdt").value=document.getElementById("datepicker3").value;
	
	$scope.type='pay';
	$scope.pnr_parent1='';	$scope.pnr_parent2='';	$scope.pnr_parent3='';
	$scope.bc = 0; 	$scope.ac = 0; 	$scope.tax_amt = 0;	 $scope.amt = 0;
	$scope.tno='';
	$scope.pnr='';
	$scope.oac = '--';
	$scope.remarks = ' ';
	$scope.venid1 = 0;	$scope.venid2 = 0;	$scope.venid3 = 0;	
	$scope.nature = 0;	
	var d = new Date();
    $scope.paydateval = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();
	$scope.tod = $scope.paydateval;
	$scope.payidval = '';	
	$scope.custid = 0;	
	$scope.parseDate=function(str)
	{			
		var mdy = str.split('-');		
		return new Date(mdy[2], mdy[1]-1, mdy[0]);		
	}
	$scope.daydiff=function(first, second)
	{		
		return (second-first)/(1000*60*60*24);	
	}
	
	
	$scope.adnew=function()
	{
		 location.reload();
	};
	$scope.fn=function()
	{	
		if(($scope.type=='pay' && $scope.nature==1 && $scope.venid1==0) || $scope.nature==0 || $scope.custid==0 || ($scope.type=='rcv' && ($scope.paydateval=='' || $scope.payidval==0)) )
			alert("Please enter all fields marked with *");
		else{		
			if(($scope.nature == '1' && $scope.type=='pay') || ($scope.nature == '15' && $scope.type=='rcv')) {
				$scope.parentadd = true;
				$scope.childshow = true;
			}
			
			var dt=document.getElementById("datepicker3").value;
			var bid=document.getElementById("bid").value;		
			var custid=document.getElementById("custid").value;		
			var venid1=$scope.venid1;		
			var venid2=$scope.venid2;		
			var venid3=$scope.venid3;		
			var remarks=$scope.remarks;		
		
			if(($scope.daydiff($scope.parseDate(dt), $scope.parseDate($scope.tod) ))>2)
			{
				alert("Transactions of date only upto 2 days prior to today are allowed");
			}
			else{
												
			var sqlparent='transaction_parent_insertdata.php?&dt='+dt+'&bid='+bid+'&custid='+custid+'&venid1='+venid1+'&venid2='+venid2+'&venid3='+venid3+'&remarks='+remarks+'&IDNumber1='+$filter('uppercase')($scope.pnr_parent1)+'&IDNumber2='+$filter('uppercase')($scope.pnr_parent2)+'&IDNumber3='+$filter('uppercase')($scope.pnr_parent3)+'&rev1='+$scope.chk1+'&rev2='+$scope.chk2+'&rev3='+$scope.chk3;
			/*alert(sqlparent);
			//single vendor
			var sqlparent='transaction_parent_insertdata.php?&dt='+dt+'&bid='+bid+'&custid='+custid+'&venid1='+venid1+'&venid2=0&venid3=0&remarks='+remarks+'&IDNumber='+$filter('uppercase')($scope.pnr_parent)+'&rev='+$scope.chk; */
			
			if(venid1 != 0)							
				sqlparent+='&netrate1='+$scope.nr1;
			if(venid2 != 0)							
				sqlparent+='&netrate2='+$scope.nr2;
			if(venid3 != 0)							
				sqlparent+='&netrate3='+$scope.nr3;
																
			if($scope.type=='rcv')				
			{
				var pd=document.getElementById('paydate');
				$scope.paydateval = pd.value;
				var cramount=document.getElementById("cramount").value;	
				var dramount=0;		
				sqlparent+='&paydate='+$scope.paydateval+'&payid='+$scope.payidval+'&cramount='+cramount;				
				//if($scope.payidval == '3'){				
					var bankid=document.getElementById("bankid").value;		
					var branch=document.getElementById("branch").value;	
					sqlparent+='&bankid='+bankid+'&branch='+branch;					
				//}				
			}	
			else
			{			
				var dramount=document.getElementById("dramount").value;
				var cramount=0;
				sqlparent+='&dramount='+dramount;
			}
			
			//for(var d=0;d<500;d++)
			$http.post(sqlparent).success(function(data)  
			{  		
				if($scope.chk1==true || $scope.chk2==true || $scope.chk3==true)
				{
					alert("Transaction and reverse credit note entry saved successfully");
					document.getElementById("save").disabled=true;		}
				else{
					alert("Transaction saved successfully");
					document.getElementById("save").disabled=true;				
					
					if($scope.sms)
					{
						var url='getsendsms_addtran.php?cid='+$scope.custid+'&amt='+$scope.Cr_Amount;					
						$http.post(url).success(function(data) {					
							$scope.sendsms(data);
							if($scope.parentadd==false)
								location.reload();
						});
					}
					if($scope.parentadd==false)
						location.reload();
				}
			});
			if($scope.parentadd==true)
			$http.post('data2.php?&x=1&rev1='+$scope.chk1+'&rev2='+$scope.chk2+'&rev3='+$scope.chk3).success(function(data) 
			{  			
				$scope.oac = data;				
				//$window.location.href = 'data2.php';
				
			});
                }
		}				
	};
	
	$scope.sendsms= function(url)
	{
		//alert("Sending SMS");
		$.post( url, function() {
		  alert( "success" );
		});				
		alert("Sending SMS");
	}
	
	$scope.fn2=function()
	{
		if($scope.parentadd == false )
			alert("Add and save the parent entry first");
		else{				
		var tno=$filter('uppercase')($scope.tno);
		var pnr=$filter('uppercase')($scope.pnr) ;
		var pax=$filter('uppercase')($scope.pax);
		var tdt=document.getElementById("tdt").value ;			
		var bdt=document.getElementById("bdt").value ;
		var rdt=document.getElementById("rdt").value ;
		var sec=$filter('uppercase')($scope.sec) ;
		var bc=$scope.bc ;
		var ac=$scope.ac ;
		var tax=$scope.tax_amt ;
		var amt=$scope.amt ;	
		
		if(!pax ||!bdt || !tdt || !sec)
			alert("Please enter all fields marked with * ");
		else{
		var sqlchild='transaction_child_insertdata.php?&tno='+tno+'&pnr='+pnr+'&pax='+pax+'&rdt='+rdt+'&tdt='+tdt+'&sec='+sec+'&bc='+bc+'&ac='+ac+'&tax='+tax+'&amt='+amt+'&bdt='+bdt+'&nature='+$scope.nature+'&rev1='+$scope.chk1+'&rev2='+$scope.chk2+'&rev3='+$scope.chk3;		
		//alert(sqlchild);	
		if($scope.nature==1)
		{
			if($scope.count==0)
				$scope.Dr_Amount=0;
			$scope.Dr_Amount+=$scope.amt;
			sqlchild+='&upddr='+$scope.Dr_Amount;
		}		
		else if($scope.nature==15)
		{
			if($scope.count==0)
				$scope.Cr_Amount=0;
			$scope.Cr_Amount+=$scope.amt;
			sqlchild+='&updcr='+$scope.Cr_Amount;
		}
		
		//alert(sqlchild);
		$http.post(sqlchild).success(function(data)  
		{     												
			alert("Entry added to transaction");
			$scope.tno=''; $scope.pnr=''; $scope.pax=''; $scope.sec='';
			document.getElementById("tdt").value=''; document.getElementById("rdt").value=''; 
			$scope.bc=0; $scope.ac=0; $scope.tax_amt=0; $scope.amt=0;			
			//document.getElementById("msg").innerHTML="Child added";
			//window.location.href = sqlchild;
		});				
		var getdata='data2.php?&x=2&rev1='+$scope.chk1+'&rev2='+$scope.chk2+'&rev3='+$scope.chk3;		
		$http.post(getdata).success(function(data,status) 
		{  			
			$scope.count++;
			//alert (JSON.stringify(data));
			$scope.datamast = data;			
			//window.location.href = getdata;
		});
		}
		}
	};	
 
 
$scope.calamt= function()
	{
		$scope.amt = $scope.bc + $scope.ac + $scope.tax_amt;
	};
	
		$scope.chgd=function()
		{
			var a=$scope.custid;
			var toc='';
			if(a=='0')
			{				
				toc="";				
			}			
			if(a.substring(0,1)=="c")
			{
				toc="Customer";			
			}
			if(a.substring(0,1)=="i")
			{
				toc="Distributor";
			}
			if(a.substring(0,1)=="d")
			{
				toc="Agent";
			}
			 if(a.substring(0,1)=="r")
			{
				toc="Corporate";
			}
			if(a.substring(0,1)=="v")
			{
				toc="Vendor";
			}
			document.getElementById('ct').innerHTML = "( "+toc+" )";
		};  
	}//end scope

	
	  function chkamt()
	  {
	  	document.frmtran.balamount.value=document.frmtran.dramount.value-document.frmtran.cramount.value;
	  } 
	
	 function chknetbuyamt()
	  { 
	  //document.getElementById('netrate').disabled = true;
	  var a=document.frmtran.venid.value;
	/*	if(a == 0) 
	   {
			document.getElementById('netfield1').innerHTML="";
			document.getElementById('netfield2').innerHTML="";
			document.getElementById('netfield3').innerHTML="";			
	   }
	   else
	   {
		document.getElementById('netfield1').innerHTML="Net Buy Rate";	
		document.getElementById('netfield2').innerHTML=":";
		document.getElementById('netfield3').innerHTML='<input name="netrate" type="number"  class="input-medium"  id="netrate"  value="0"  placeholder="00" />	';		
		} 	*/	
	  } 

 /*   function chkdramt()
      {
    	if (document.frmtran.dramount.value>0)
		{
			document.getElementById('bankid').disabled = true;
			document.getElementById('branch').disabled = true;
            document.getElementById('paydate').disabled = true;
            document.getElementById('payid').disabled = true;
		}
		else
		{
			document.getElementById('bankid').disabled = false;
			document.getElementById('branch').disabled = false;
            document.getElementById('paydate').disabled = false;
            document.getElementById('payid').disabled = false;
		}
	  }

 function chkcramt()
      {
        if (document.frmtran.cramount.value>0)
		{
			document.getElementById('bankid').disabled = false;
          	document.getElementById('branch').disabled = false;
            document.getElementById('paydate').disabled = false;
            document.getElementById('payid').disabled = false;
		}
		else
		{
			document.getElementById('bankid').disabled = true;
			document.getElementById('branch').disabled = true;
            document.getElementById('paydate').disabled = true;
            document.getElementById('payid').disabled = true;
		}
	  }


	  function validatefrm()
	  {
		if (document.frmtran.payid.value==2)
		{
			if (document.frmtran.bankid.value==1)
			{
				alert("select Bank name");
				document.frmtran.bankid.focus();
				return false;
			}	
			if (document.frmtran.paydate.value=="")
			{
				alert("select Pay Date");
				document.frmtran.paydate.focus();
				return false;
			}	
			if (document.frmtran.branch.value=="")
			{
				alert("Enter Branch Name");
				document.frmtran.branch.focus();
				return false;
			}	
		}	
	  }*/

	